package com.lanren.hovertitlerecylerview;

import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * @ClassName HoverTitleRecyclerView
 * @Description TODO RecyclerView 分组与悬停效果
 * @Author chongheng.wang
 * @Date 2019/8/9 14:23
 * @Version 1.0
 */
public class HoverTitleRecyclerView extends RecyclerView {

    public interface OnHoverTitleListener {

        void onHoverTitleClick(int adapterPosition);

        void onHoverTitleSelection(int adapterPosition);
    }

    public HoverTitleRecyclerView(Context context) {
        super(context);
    }

    public HoverTitleRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HoverTitleRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private OnHoverTitleListener mOnHoverTitleListener;

    public void setmOnHoverTitleListener(OnHoverTitleListener mOnHoverTitleListener) {
        this.mOnHoverTitleListener = mOnHoverTitleListener;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
//        if (mHoverTitleClickListener == null) {
//            return super.onInterceptTouchEvent(e);
//        }
//        IHoverTitleDecoration hoverTitleInterface = getHoverTitleDecoration();
//        if (hoverTitleInterface == null) {
//            return super.onInterceptTouchEvent(e);
//        }
//        Rect hoverTitleRect = hoverTitleInterface.getPinnedHeaderRect();
//        int hoverTitlePosition = hoverTitleInterface.getPinnedHeaderPosition();
//        if (hoverTitleRect == null || hoverTitlePosition == -1) {
//            return super.onInterceptTouchEvent(e);
//        }
        return super.onInterceptTouchEvent(e);
    }


    /**
     * 如果有固定的header的情况
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
//        if (mHoverTitleClickListener == null) {
//            return super.onTouchEvent(ev);
//        }
//        IHoverTitleDecoration hoverTitleInterface = getHoverTitleDecoration();
//        if (hoverTitleInterface == null) {
//            return super.onTouchEvent(ev);
//        }
//        Rect hoverTitleRect = hoverTitleInterface.getPinnedHeaderRect();
//        int hoverTitlePosition = hoverTitleInterface.getPinnedHeaderPosition();
//        if (hoverTitleRect == null || hoverTitlePosition == -1) {
//            return super.onTouchEvent(ev);
//        }
        return super.onTouchEvent(ev);
    }

    private boolean isScroll;

    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);

        if (RecyclerView.SCROLL_STATE_DRAGGING == state || RecyclerView.SCROLL_STATE_SETTLING == state) {
            isScroll = true;
        }
        if (RecyclerView.SCROLL_STATE_IDLE == state) {
            if (isScroll) {
                isScroll = false;
                if (mOnHoverTitleListener == null) {
                    return;
                }

                IHoverTitleDecoration hoverTitleInterface = getHoverTitleDecoration();
                if (hoverTitleInterface == null) {
                    return;
                }
                Rect hoverTitleRect = hoverTitleInterface.getHoverTitleRect();
                int hoverTitlePosition = hoverTitleInterface.getHoverTitlePosition();
                if (hoverTitleRect == null || hoverTitlePosition == -1) {
                    return;
                }

                mOnHoverTitleListener.onHoverTitleSelection(hoverTitlePosition);
            }
        }

    }

    public IHoverTitleDecoration getHoverTitleDecoration() {
        int decorationIndex = 0;
        ItemDecoration itemDecoration;
        do {
            itemDecoration = getItemDecorationAt(decorationIndex);
            if (itemDecoration instanceof IHoverTitleDecoration) {
                return (IHoverTitleDecoration) itemDecoration;
            }
            decorationIndex++;
        } while (itemDecoration != null);
        return null;
    }
}
